package com.funtl.itoken.service.posts.controller;

import com.funtl.itoken.common.domain.TbPostsPost;
import com.funtl.itoken.common.dto.BaseResult;
import com.funtl.itoken.common.utils.MapperUtils;
import com.funtl.itoken.service.posts.service.TbPostsPostService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("v1/posts")
public class PostsController {
    @Autowired
    private TbPostsPostService tbPostsPostService;

    @GetMapping("/{postGuid}")
    public BaseResult page(@PathVariable("postGuid")String postGuid){
        TbPostsPost tbPostsPost = new TbPostsPost();
        tbPostsPost.setPostGuid(postGuid);
        TbPostsPost baseDomain = (TbPostsPost) tbPostsPostService.selectOne(tbPostsPost);
        return BaseResult.ok(baseDomain);
    }

    @PostMapping("")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbPostsPostJson", value = "管理员对象 JSON 字符串", required = false, dataTypeClass = TbPostsPost.class, paramType = "json")
    })
    public BaseResult save(@RequestParam(required = true) String tbPostsPostJson,
                          @RequestParam(required = true) String optsBy){

        TbPostsPost tbPostsPost=null;

        int result=0;

        try {
            tbPostsPost = MapperUtils.json2pojo(tbPostsPostJson, TbPostsPost.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (tbPostsPost!=null){
            // 主键为空为新增
            if(StringUtils.isBlank(tbPostsPost.getPostGuid())){
                result= tbPostsPostService.insert(tbPostsPost, optsBy);
            }else{
                // 主键有值为修改
                result= tbPostsPostService.update(tbPostsPost, optsBy);
            }
        }

        if(result>0){
            return BaseResult.ok("保存文章成功");
        }
        return BaseResult.ok("保存文章失败");
    }


    @GetMapping("page/{pageNum}/{pageSize}")
    public BaseResult page(@PathVariable(required = true) int pageNum,
                           @PathVariable(required = true) int pageSize,
                           @RequestParam(required = false) String tbPostsPostJson){

        TbPostsPost tbPostsPost=null;
        if(StringUtils.isNotBlank(tbPostsPostJson)){
            try {
                tbPostsPost = MapperUtils.json2pojo(tbPostsPostJson, TbPostsPost.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PageInfo<TbPostsPost> tbPostsPostPageInfo = tbPostsPostService.selectPage(pageNum, pageSize, tbPostsPost);
        BaseResult.Cursor cursor = new BaseResult.Cursor();
        cursor.setTotal(new Long(tbPostsPostPageInfo.getTotal()).intValue());
        cursor.setOffset(tbPostsPostPageInfo.getPages());
        cursor.setLimit(tbPostsPostPageInfo.getPageSize());

        List<TbPostsPost> list = tbPostsPostPageInfo.getList();

        return BaseResult.ok(list,cursor);
    }
}


